<?php
@session_start();
require_once('config/connection.php');
require_once('utils/CommonUtils.php');
require_once('utils/DBUtils.php');
require_once('application/model/BaseDAO.php');
require_once('application/model/UserMenuDAO.php');
require_once("lib/core/Session.php");
require_once("lib/core/Object.php");
$commonUtils = new CommonUtils();
$baseDAO = new BaseDAO();
$userMenuDAO = new UserMenuDAO();

$userId = Session::get('userid');
$onlineUser = Session::get('username');

if($onlineUser == 'admin'){
	$topmenus = $baseDAO->getItems("topmenu", array("id", "name", "targetid", "url", "icon"), "active = 1", "ORDER BY orderdisplay ASC");	
}else{
	$topmenus = $userMenuDAO->loadTopMenuForUser($userId);
}

?>
<div class="header">
	<div class="menu">
		<div class="menuContent">
			<div class="mainmenu">
				<?php
					foreach ($topmenus as $topmenu){
				?>
				<?php if(isset($topmenu["targetid"]) && $topmenu["targetid"] != null){?>
					<a onclick="openTab('<?php echo $topmenu["targetid"]?>','application/controller/<?php echo $topmenu["url"]?>','<?php echo $topmenu["name"]?>');">
				<?php }?>
					<div style="<?php if(isset($topmenu["targetid"]) && $topmenu["targetid"] != null){?>cursor:pointer<?php }else{?>cursor:default<?php }?>" id="<?php echo $topmenu["id"]?>" <?php if(isset($topmenu["targetid"]) && $topmenu["targetid"] != null){?>onclick="mainSelectedA(this);"<?php }?> onmouseover="sdi_Navigation('<?php echo $topmenu["id"]?>');" onmouseout="sdi_subHide();" >
						<img src="<?php echo $topmenu["icon"]?>"/><br/>
						<?php echo $topmenu["name"]?>
					</div>
				<?php if(isset($topmenu["targetid"]) && $topmenu["targetid"] != null){?>
					</a>
				<?php }?>
				<?php } ?>
			</div>
			<div class="clear"></div>
		</div>
		
		<div class="search">
			<div class="login">
				<div id="d1" style="float:left;">
					<a href="#" onClick="showProfileForm();" style='cursor:pointer;color:#0076C4;'>
						<div style="float:left"><img id="imgIcon" src="images/user.png" border="0"/></div>
						<div style="float:left;">&nbsp;<?php echo Session::get('fullname'); ?></div>
					</a>
				</div>
				<div style="float:left;width:5px;">&nbsp;</div>
				<div>
					<a href="login.php?action=logout" style='cursor:pointer'>
						<div style="float:left">
							<img id="imgIcon" src="images/stop.png" border="0"/>
						</div>
						<div style="float:left;color:#0076C4;">
							&nbsp;Thoát
						</div>
					</a>	
				</div>
			</div>
			<div class="clear"></div>
		</div>
	</div>
</div>

<div class="submainmenu">
	<?php
		$loaded = false;
		foreach ($topmenus as $topmenu){
	?>
	<div style="display: none; float:left; text-align: center;" id="sub<?php echo $topmenu["id"]?>" onmouseover="sdi_subShowContinued();" onmouseout="sdi_subHide();">
		<?php
			if($onlineUser == 'admin'){
				$whereClause = "active = 1 and topmenuid = ".$topmenu['id'];
				$menus = $baseDAO->getItems("menu", array("id", "topmenuid", "name", "targetid", "url"), $whereClause, "ORDER BY orderdisplay ASC");
			}else{
				$menus = $userMenuDAO->loadMenuForUser($userId, $topmenu['id']);
			}
			 $index = 0;
			 foreach ($menus as $menu){
		?>
			<?php 
				if($index != 0)
					echo "|";
				$index++;
			?>
			<a id="sub<?php echo $menu["topmenuid"]?>_<?php echo $menu["id"]?>" 
				href="javascript:void(0)" 
				onclick="mainSelectedB('<?php echo $topmenu["id"]?>','sub<?php echo $topmenu["id"]?>_<?php echo $menu["id"]?>');
				openTab('<?php echo $menu["targetid"]?>','application/controller/<?php echo $menu["url"]?>','<?php echo $menu["name"]?>', '<?php if(isset($menu["role"]) && $menu["role"] != null) echo $menu["role"]; else if($onlineUser == 'admin') echo '2'; else echo '0';?>');
				"><?php echo $menu["name"]?></a>
			<?php if (strpos($menu["url"],".php")){?>
				<script type="text/javascript" src="application/view/<?php echo str_replace(".php", ".js", $menu["url"])?>" ></script>
				<script type="text/javascript" src="resources/<?php echo 're_'.str_replace(".php", ".js", $menu["url"])?>" ></script>
			<?php }?>
			<?php if(!$loaded && strpos($menu["url"],".php")){
					$loadFirst = array();
					$loadFirst["targetid"] = $menu["targetid"];
					$loadFirst["url"] = $menu["url"];
					$loadFirst["name"] = $menu["name"];
					$loadFirst["id"] = $menu["id"];
					$loadFirst["topmenuid"] = $menu["topmenuid"];
					$loadFirst["role"] =  (isset($menu["role"]) && $menu["role"] != null)? $menu["role"] : ($onlineUser == 'admin' ? '2' : '0');
					$loaded = true;
			 } ?>
		<?php } ?>
	</div>
	<?php } ?>
	
	<!-- Load password and profile form if not admin user -->
	<?php if($onlineUser != 'admin'){ ?>
		 <script type="text/javascript" src="application/view/user.js" ></script>
	<?php } ?>
</div>
<?php if($loaded){?>
 <script type="text/javascript">
	window.onload = function(){
		mainSelectedB(<?php echo $loadFirst["topmenuid"] ?>, 'sub<?php echo $loadFirst["topmenuid"] ?>_<?php echo $loadFirst["id"]?>');
		document.getElementById('sub<?php echo $loadFirst["topmenuid"]?>').style.display = 'block';
	}
</script>
<?php }?>
